#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <sys/time.h>
 
#include "data_type.h"
#include "cube.h"
#include "cube_define.h"
#include "cube_record.h"

#include "user_define.h"
#include "node_define.h"
#include "data_define.h"

#include "event_send.h"
// add para lib_include

char Buf[DIGEST_SIZE*4];

int event_send_init(void * sub_proc, void * para)
{
	int ret;
	// add yorself's module init func here
	return 0;
}

int event_send_start(void * sub_proc, void * para)
{
	int ret;
	void * recv_msg;
	int type;
	int subtype;
	// add yorself's module exec func here
	while(1)
	{
		usleep(time_val.tv_usec);
		ret=ex_module_recvmsg(sub_proc,&recv_msg);
		if(ret<0)
			continue;
		if(recv_msg==NULL)
			continue;
		type=message_get_type(recv_msg);
		subtype=message_get_subtype(recv_msg);
		if(!memdb_find_recordtype(type,subtype))
		{
			printf("message format (%d %d) is not registered!\n",
			message_get_type(recv_msg),message_get_subtype(recv_msg));
			continue;
		}
		if((type==TYPE(NODE_DEFINE))&&(subtype==SUBTYPE(NODE_DEFINE,CONFIG)))
		{
			ret=proc_event_nodeconfig(sub_proc,recv_msg);
		}
		if((type==TYPE(NODE_DEFINE))&&(subtype==SUBTYPE(NODE_DEFINE,USER)))
		{
			ret=proc_event_nodeuser(sub_proc,recv_msg);
		}
		if((type==TYPE(EVENT_DEFINE))&&(subtype==SUBTYPE(EVENT_DEFINE,EVENT_CONTROL)))
		{
			ret=proc_event_send_control(sub_proc,recv_msg);
		}
	}
	return 0;
}

int proc_event_nodeconfig(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(USER_DEFINE,CLIENT_STATE) * client_state;
	RECORD(NODE_DEFINE,CONFIG) * node_config;
	RECORD(EVENT_DEFINE,EVENT_CONTROL) * event_control;
	void * new_msg;

	ret=message_get_record(recv_msg,&node_config,0);
	if(ret<0)
		return ret;
    // get name from login_input's name
    // the input info is user_name:role, while role can be SYSADMIN, USER,etc

	client_state = proc_share_data_getpointer();

	event_control=Talloc0(sizeof(*event_control));
	if(event_control==NULL)
		return -ENOMEM;
	
   // fill the event control struct  
   event_control->controller = dup_str(client_state->name,DIGEST_SIZE);
   event_control->node_name = dup_str(node_config->node_name,DIGEST_SIZE);
   event_control->command = CTRL_CONFIG;
   event_control->start_time=0;

    // build event control message 
	new_msg=message_create(TYPE_PAIR(EVENT_DEFINE,EVENT_CONTROL),recv_msg);	
	if(new_msg==NULL)
		return -EINVAL;
	ret=message_add_record(new_msg,event_control);
	if(ret<0)
		return ret;

    // add node_config message as message expand 
	ret=message_add_expand_data(new_msg,TYPE_PAIR(NODE_DEFINE,CONFIG),node_config);
    if(ret<0)
        return -EINVAL;

	ret=ex_module_sendmsg(sub_proc,new_msg);
	return ret;
}

int proc_event_nodeuser(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(USER_DEFINE,CLIENT_STATE) * client_state;
	RECORD(NODE_DEFINE,USER) * node_user;
	RECORD(EVENT_DEFINE,EVENT_CONTROL) * event_control;
	void * new_msg;

	ret=message_get_record(recv_msg,&node_user,0);
	if(ret<0)
		return ret;
    // get name from login_input's name
    // the input info is user_name:role, while role can be SYSADMIN, USER,etc

	client_state = proc_share_data_getpointer();

	event_control=Talloc0(sizeof(*event_control));
	if(event_control==NULL)
		return -ENOMEM;
	
   // fill the event control struct  
   event_control->controller = dup_str(client_state->name,DIGEST_SIZE);
   event_control->node_name = dup_str(node_user->node_name,DIGEST_SIZE);
   event_control->command = CTRL_ADDUSER;
   event_control->start_time=0;

    // build event control message 
	new_msg=message_create(TYPE_PAIR(EVENT_DEFINE,EVENT_CONTROL),recv_msg);	
	if(new_msg==NULL)
		return -EINVAL;
	ret=message_add_record(new_msg,event_control);
	if(ret<0)
		return ret;

    // add node_config message as message expand 
	ret=message_add_expand_data(new_msg,TYPE_PAIR(NODE_DEFINE,USER),node_user);
    if(ret<0)
        return -EINVAL;

	ret=ex_module_sendmsg(sub_proc,new_msg);
	return ret;
}

int proc_event_send_control(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(USER_DEFINE,CLIENT_STATE) * client_state;
	RECORD(EVENT_DEFINE,EVENT_CONTROL) * event_control;
	void * new_msg;

	ret=message_get_record(recv_msg,&event_control,0);
	if(ret<0)
		return ret;
    // get name from login_input's name
    // the input info is user_name:role, while role can be SYSADMIN, USER,etc

	client_state = proc_share_data_getpointer();

   // fill the event control struct  
   event_control->controller = dup_str(client_state->name,DIGEST_SIZE);

    // build event control message 
	new_msg=message_create(TYPE_PAIR(EVENT_DEFINE,EVENT_CONTROL),recv_msg);	
	if(new_msg==NULL)
		return -EINVAL;
	ret=message_add_record(new_msg,event_control);
	if(ret<0)
		return ret;
	ret=ex_module_sendmsg(sub_proc,new_msg);
	return ret;
}
